package com.mezizy.ifaner;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;

import com.mezizy.ifaner.global.Config;
import com.mezizy.ifaner.global.Global;


public class SignUpActivity extends ActionBarActivity {
    EditText editMoodleId, editMoodlePwd, editiFanerId, editiFanerPwd,editiFanerUserName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up_moodle_authorize);

        editMoodleId = (EditText) findViewById(R.id.editSignUpMoodleId);
        editMoodlePwd = (EditText) findViewById(R.id.editSignUpMoodlePwd);

//        editiFanerId.setText(Global.moodleId);

    getSupportActionBar().hide();
    }


    public void btnSignUpCreateClicked(View v) {
        editiFanerUserName=(EditText)findViewById(R.id.editSignUpiFanerUserName);

        final String iFanerId = editiFanerId.getText().toString();
        final String iFanerPwd = editiFanerPwd.getText().toString();
        final String iFanerUserName = editiFanerUserName.getText().toString();

        if (iFanerPwd.length()<Config.PASSWORD_LENGTH ){
            Global.showToast("密碼至少需要"+Config.PASSWORD_LENGTH+"個字符");
            return;
        }
        if (iFanerUserName.equals("")){
            Global.showToast("未輸入暱稱");
            return;
        }
//
//        final Map<String, String> data = new HashMap<>();
//        data.put("iFanerId", iFanerId);
//        data.put("iFanerPwd", iFanerPwd);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
//                    String result=(String) Global.postWebService(data, Config.IFANER_SIGN_UP_URI, String.class);
                    if (Global.addUser(iFanerId, Global.encrypt(iFanerPwd), iFanerUserName)){
                        Global.showToast("註冊成功！", SignUpActivity.this);
                        Global.userId =iFanerId;
                        Global.userPwdSha =Global.encrypt(iFanerPwd);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                startActivity(new Intent(SignUpActivity.this, MainActivity.class));
                            }
                        });
                    }else{
                        Global.showToast("註冊失敗！", SignUpActivity.this);

                    }

                } catch (Exception e) {
                    Global.showToast("註冊失敗！", SignUpActivity.this);
                }
            }
        }).start();


    }


    public void btnSignUpAuthClicked(View v) {
        final String moodleId = editMoodleId.getText().toString();
        final String moodlePwd = editMoodlePwd.getText().toString();
        SignUpActivity.this.setProgressBarVisibility(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                /*
                test //TODO 注册，等待删除
                 */
                if (moodleId.equals("zizy")) {
                    Global.showToast("認證成功！", SignUpActivity.this);
                    Global.moodleId = moodleId;
                    Global.moodlePwd = moodlePwd;
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            setContentView(R.layout.activity_sign_up_create_user);


                            editiFanerId = (EditText) findViewById(R.id.editSignUpiFanerId);
                            editiFanerPwd = (EditText) findViewById(R.id.editSignUpiFanerPwd);

                            editiFanerId.setText(Global.moodleId);

                        }


                    });

                    return;
                }


                if (Global.moodleAuthorize(moodleId, moodlePwd) == Config.MOODLE_LOG_IN_SUCCESS) {
                    Global.moodleId = moodleId;
                    Global.moodlePwd = moodlePwd;

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Global.showToast("認證成功！", SignUpActivity.this);
                            SignUpActivity.this.setProgressBarVisibility(false);
                            setContentView(R.layout.activity_sign_up_create_user);


                            editiFanerId = (EditText) findViewById(R.id.editSignUpiFanerId);
                            editiFanerPwd = (EditText) findViewById(R.id.editSignUpiFanerPwd);

                            editiFanerId.setText(Global.moodleId);

                        }
                    });
                } else if (Global.moodleAuthorize(moodleId, moodlePwd) == Config.MOODLE_LOG_IN_FAILED) {
                    Global.showToast("認證失敗！", SignUpActivity.this);
                }
            }
        }).start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_sign_up, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
